package net.easyjava.framework.web.security;

import net.easyjava.tools.http.request.RequestTools;
import net.easyjava.tools.lang.StringTools;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 将用户数据经过双重加密放入到 response 中,具体方法需要子类重写
 * Created by wxnacy on 16/3/7.
 */
@Service
public class UserSecurityOnHead extends UserSecurity {


    /**
     * 添加长期不过期用户数据
     *
     * @param userId
     * @param status
     * @param request
     * @param response
     */
    @Override
    public void setLongUserSecurityBean(String userId, int status, HttpServletRequest request, HttpServletResponse response) {
        this.setUserSecurityBean(userId, status, request, response);
    }

    /**
     * 将加密信息写入 response 中
     *
     * @param response
     * @param value
     */
    @Override
    protected void writeUserSecurity(HttpServletResponse response, String value) {
        response.setHeader(UserSecurityConstants.KEY_HEAD,value);
    }

    /**
     * 获取 request 中的加密信息
     *
     * @param request
     * @return
     */
    @Override
    protected String readUserSecurity(HttpServletRequest request) {
        String UI = RequestTools.getHeader(request, UserSecurityConstants.KEY_HEAD);

        if(StringTools.isEmpty(UI)){
            UI = RequestTools.getParam(request,UserSecurityConstants.KEY_HEAD);
        }
        return UI;
    }
}
